/*
 * Created on 2006-7-30
 *
 */

package com.hd123.rumba.action.log;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.sql.ResultPage;
import com.hd123.rumba.sql.SQLSubquery;
import com.hd123.rumba.util.StringUtil;

/**
 * @author cwf
 */
public class ViewOperateLogs extends ActionAbstractImpl implements Action {
  public String execute() throws Exception {
    String startnode = (String) dd.getFormValue("start");
    String url = ActionConstants.NOTIMPLEMENTED;
    if (startnode == null)
      startnode = "list";
    if (startnode.equals("list"))
      url = executeWithPermission("list", PERMISSION_OPERATORLOG_VIEW);
    else if ("dispatch".equals(startnode)) {
      if (dd.getFormValues().containsKey("query"))
        url = executeWithPermission("query", PERMISSION_OPERATORLOG_VIEW);
      else
        url = paging();
    }
    return url;
  }

  protected final String TEMPLATE_LIST = "rumba/log/OperateLogsList.jsp";
  public static final String PERMISSION_OPERATORLOG_VIEW = "操作日志.查看权";

  public String list() throws Exception {
    dd.putFormValue("PageNumber_0", "0");
    Calendar calendar = new GregorianCalendar();
    int day = calendar.get(Calendar.DATE);
    int month = calendar.get(Calendar.MONTH) + 1;
    int year = calendar.get(Calendar.YEAR);
    dd.putFormValue("cond_fromDate", year + "-" + month + "-" + day);
    dd.putFormValue("cond_toDate", year + "-" + month + "-" + day);
    dd.putFormValue("cond_logType", "all");
    dd.putFormValue("cond_useType", "user");
    return paging();
  }

  public String query() throws Exception {
    dd.putFormValue("PageNumber_0", "0");
    return paging();
  }

  public String paging(int page) throws Exception {
    dd.validateFormValue("cond_fromDate", "Date", false);
    dd.validateFormValue("cond_toDate", "Date", false);
    if (!dd.isFormValid()) {
      if (dd.getInvalidFormValues().containsKey("cond_fromDate_invalid"))
        dd.addErrorText("操作时间从格式不对, 正确格式为yyyy-MM-dd.");
      if (dd.getInvalidFormValues().containsKey("cond_toDate_invalid"))
        dd.addErrorText("操作时间到格式不对, 正确格式为yyyy-MM-dd.");
      dd.setWorkingTemplate(TEMPLATE_LIST);
      return ActionConstants.APPLICATIONFRAME;
    }
    SQLSubquery sq = genLogSQLSubqueryByCondition();
    String sortField = (String) dd.getFormValue("cond_sort_field");
    if (sortField == null) {
      sortField = "ol.time";
      dd.putFormValue("cond_sort_field", "ol.time");
    }
    String sortOrder = (String) dd.getFormValue("cond_sort_order");
    if (sortOrder == null) {
      sortOrder = "desc";
      dd.putFormValue("cond_sort_order", "desc");
    }
    if (sortField.equals("operatortime")) {
      sq.addOrder("ol.operator", "asc");
      sq.addOrder("ol.time", sortOrder);
    } else
      sq.addOrder(sortField, sortOrder);
    ResultPage rp = ResultPage.createJdbcResultPage(HibernateSession.getSession().connection(), sq,
        15, page);
    dd.put("logs", rp.getContentList());
    dd.put("pageInfo", rp);
    dd.setWorkingTemplate(TEMPLATE_LIST);
    return ActionConstants.APPLICATIONFRAME;

  }

  public SQLSubquery genLogSQLSubqueryByCondition() throws Exception {

    String operator = (String) dd.getFormValue("cond_operator");
    String logType = (String) dd.getFormValue("cond_logType");
    String temp = (String) dd.getFormValue("cond_fromDate");
    Date fromDate = null;
    if (temp != null && temp.length() != 0)
      fromDate = DateFormat.getDateInstance().parse(temp);

    temp = (String) dd.getFormValue("cond_toDate");
    Date toDate = null;
    if (temp != null && temp.length() != 0)
      toDate = DateFormat.getDateInstance().parse(temp);
    String actionCaption = (String) dd.getFormValue("cond_actionCaption");
    SQLSubquery sq = new SQLSubquery();

    if (logType.equals("all")) {
      sq
          .addSelect("ol.uuid, d.code, d.name, ol.operator, ol.time, ol.event, ol.message, al.actionCaption, al.actionClass, "
              + "el.managerCaption, el.managerClass, el.entityCaption, el.entityUuid, el.oldState, el.newState, "
              + "ecl.fieldCaption, ecl.fieldName, ecl.oldValue, ecl.newValue");
      sq
          .addFrom("rboperatelog ol, rbactionlog al, rbentitylog el, rbentitychangelog ecl, tangodomain d");
      sq.addWhere("ol.uuid = al.uuid(+) and ol.uuid = el.uuid(+) and ol.uuid = ecl.uuid(+)");
    } else if (logType.equals("action")) {
      sq
          .addSelect("ol.uuid, d.code, d.name, ol.operator, ol.time, ol.event, ol.message, al.actionCaption, al.actionClass");
      sq.addFrom("rboperatelog ol, rbactionlog al, tangodomain d");
      sq.addWhere("ol.uuid = al.uuid");
    } else {
      sq
          .addSelect("ol.uuid, d.code, d.name, ol.operator, ol.time, ol.event, ol.message, "
              + "el.managerCaption, el.managerClass, el.entityCaption, el.entityUuid, el.oldState, el.newState, "
              + "ecl.fieldCaption, ecl.fieldName, ecl.oldValue, ecl.newValue");
      sq.addFrom("rboperatelog ol, rbentitylog el, rbentitychangelog ecl, tangodomain d");
      sq.addWhere("ol.uuid = el.uuid and el.uuid = ecl.uuid(+)");
    }
    sq.addWhere("and ol.domain = d.uuid and (ol.domain = ?", dd.getCurrentDomain().getUuid());
    sq.addWhere("or d.upperdomain = ?)", dd.getCurrentDomain().getUuid());
    if (operator != null && operator.trim().length() != 0)
      sq.addWhere("and ol.operator like ?", operator.trim().toUpperCase(), SQLSubquery.WC_BOTH);
    if (fromDate != null)
      sq.addWhere("and ol.time >= ?", fromDate);
    if (toDate != null)
      sq.addWhere("and ol.time < ?", StringUtil.addDate(toDate, Calendar.DATE, 1));
    if (actionCaption != null && actionCaption.length() != 0) {
      if (logType.equals("all") || logType.equals("action"))
        sq.addWhere("and al.actionCaption like ?", actionCaption, SQLSubquery.WC_BOTH);
      else
        sq.addWhere("and el.managerCaption like ?", actionCaption, SQLSubquery.WC_BOTH);
    }
    return sq;
  }
}
